package com.turing.post.salesmanage.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.salesmanage.util.SaleOrder;
import com.turing.post.salesmanage.util.SaleOrderDao;
import com.turing.post.salesmanage.util.SaleOrderSalesDetail;
import com.turing.post.salesmanage.util.SaleOrderSalesDetailBusiness;
import com.turing.post.salesmanage.util.SaleOrderSalesDetailDao;
import com.turing.post.salesmanage.util.SalesManageApplicationException;
import com.turing.post.salesmanage.util.SalesManageRespCode;
import com.turing.post.util.PCPSApplicationException;

/**
 * 销售单销售员明细
 * 
 * @author zxp
 * @Dec 23, 2012
 */
@Stateless(mappedName = "salesmanage.impl.SaleOrderSalesDetailBusinessImpl")
@Remote
@Local
public class SaleOrderSalesDetailBusinessImpl implements
		SaleOrderSalesDetailBusiness {

	@EJB(mappedName = "salesmanage.impl.SaleOrderSalesDetailDaoImpl")
	private SaleOrderSalesDetailDao saleOrderSalesDetailDaoImpl;

	@EJB(mappedName = "salesmanage.impl.SaleOrderDaoImpl")
	private SaleOrderDao saleOrderDaoImpl;

	@Override
	public void insertSaleOrderSalesDetailInfo(
			SaleOrderSalesDetail saleOrderSalesDetail)
			throws PCPSApplicationException {
		// 业务判断
		if (saleOrderSalesDetail == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"SaleOrderSalesDetail为NULL");
		}
		// 双主键
		if (saleOrderSalesDetail.getSalesId() == null
				|| saleOrderSalesDetail.getSaleOrderId() == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"SaleOrderId为NULL或SalesId为NULL");
		}

		if (saleOrderSalesDetailDaoImpl
				.getSaleOrderSalesDetailByPrimaryKey(saleOrderSalesDetail) != null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"该销售单下销售员已经添加!");
		}

		// 录入销售员明细时判断销售单状态,只有录入状态才可添加
		SaleOrder saleOrder = saleOrderDaoImpl
				.getSaleOrderForUpdate(saleOrderSalesDetail.getSaleOrderId());
		if (!saleOrder.getOrderStatus().equals(
				SaleOrder.SALEORDER_ORDERSTATUS_INPUT)) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
					"只允许在录入状态下添加销售员!");
		}
		// 本单贡献度
//		if (saleOrderSalesDetail.getDegreeOfcontribution() == null) {
//			throw new SalesManageApplicationException(
//					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "本单贡献度为NULL");
//		}
		saleOrderSalesDetailDaoImpl
				.insertSaleOrderSalesDetail(saleOrderSalesDetail);
	}

//	@Override
//	public void updateSaleOrderSalesDetailInfo(
//			SaleOrderSalesDetail saleOrderSalesDetail)
//			throws PCPSApplicationException {
//		// 业务判断
//		if (saleOrderSalesDetail == null) {
//			throw new SalesManageApplicationException(
//					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
//					"SaleOrderSalesDetail为NULL");
//		}
//		// 双主键
//		if (saleOrderSalesDetail.getSalesId() == null
//				|| saleOrderSalesDetail.getSaleOrderId() == null) {
//			throw new SalesManageApplicationException(
//					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
//					"SaleOrderId为NULL或SalesId为NULL");
//		}
//		// 录入销售员明细时判断销售单状态,只有录入状态才可添加
//		SaleOrder saleOrder = saleOrderDaoImpl
//				.getSaleOrderForUpdate(saleOrderSalesDetail.getSaleOrderId());
//		if (!saleOrder.getOrderStatus().equals(
//				SaleOrder.SALEORDER_ORDERSTATUS_INPUT)) {
//			throw new SalesManageApplicationException(
//					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
//					"只允许在录入状态下修改销售员!");
//		}
//		// 本单贡献度
//		if (saleOrderSalesDetail.getDegreeOfcontribution() == null) {
//			throw new SalesManageApplicationException(
//					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					SalesManageRespCode.SALESBUSINESS_ARGS_NULL, "本单贡献度为NULL");
//		}
//		saleOrderSalesDetailDaoImpl
//				.updateSaleOrderSalesDetail(saleOrderSalesDetail);
//	}

	@Override
	public void deleteSaleOrderSalesDetailInfo(
			SaleOrderSalesDetail saleOrderSalesDetail)
			throws PCPSApplicationException {
		SaleOrderSalesDetail sosd = saleOrderSalesDetailDaoImpl
				.getSaleOrderSalesDetailByPrimaryKey(saleOrderSalesDetail);
		if (sosd != null) {
			// 判断销售单状态,只有录入状态才可执行
			SaleOrder saleOrder = saleOrderDaoImpl
					.getSaleOrderBySaleOrderId(saleOrderSalesDetail
							.getSaleOrderId());
			if (!saleOrder.getOrderStatus().equals(
					SaleOrder.SALEORDER_ORDERSTATUS_INPUT)) {
				throw new SalesManageApplicationException(
						SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
						SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
						"只允许在录入状态下删除销售员!");
			}
			Integer deleteCount = saleOrderSalesDetailDaoImpl
					.deleteSaleOrderSalesDetail(sosd);
			if (deleteCount < 0) {
				throw new SalesManageApplicationException(
						SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
						SalesManageRespCode.SALESBUSINESS_ARGS_NULL,
						"数据库异常，销售单销售员删除失败!");
			}
		}
	}
}
